package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.ChuangshiParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class ChuangshiCrawl extends BaseCrawl {

	private static HttpUtil httpBook = new HttpUtil("UTF-8");
	private static HttpUtil httpGBKBook = new HttpUtil("GBK");
	private static Logger logger = Logger.getLogger(ChuangshiCrawl.class);

	public ChuangshiCrawl() {
		tag = Book.TYPE_CHUANGSHI;
	}

	/**
	 * 爬一本书,目录
	 * 
	 * @param bookId
	 */
	public void crawlBook(Book book) {
		String page = "";
		try {
			if (ClassUtil.isEmpty(book.describ)) { 
				//http://chuangshi.qq.com/bk/xh/59967446-1.html
				String infoUrl = "http://chuangshi.qq.com/bk/xh/" + book.webId + ".html";
				logger.info("介绍信息:" + infoUrl);
				page = httpBook.getPage(infoUrl);
				ChuangshiParser.parseDescrib(page, book);
			}
			//http://chuangshi.qq.com/read/bk/ds/97032054-m.html
			String categoryUrl = "http://chuangshi.qq.com/bk/xh/" + book.webId + "-l.html";
			logger.info("读取目录:" + categoryUrl);
			page = httpBook.getPage(categoryUrl);
			List<Chapter> list = ChuangshiParser.parseBook(page, book);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}

	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		//dir_num=Math.floor(cid/1000) // 目录=cid/1000
		//http://chuangshi.qq.com/www/static/1334/1334409.html
//		String dir = Integer.parseInt(chapter.webId)/1000 +"";
		String urlStr = "http://chuangshi.qq.com/index.php/Bookreader/"+chapter.webId;
		logger.info("爬取章节 :[" + urlStr + "]");
		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpBook.getPage(urlStr);
			} catch (Exception e) {
				logger.error(e);
			}
		} 		
		ChuangshiParser.parseChapter(page, chapter);
		chapter.chapterContent = filter(chapter.chapterContent);
		logger.debug(chapter.chapterContent);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) { 
		page=page.replaceFirst("<p style='.+", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args)throws Exception {
		Book book = new Book();
		book.webId = "456867";//26719615
		ChuangshiCrawl crawl = new ChuangshiCrawl();
		crawl.crawlBook(book);
		 
	}

}
